package logic.gridNodes;

import eduni.simjava.Sim_entity;
import java.util.LinkedList;
import logic.gridSimInterface.brokers.BrokerHierarchical;
import logic.topology.NetworkNode;

/**
 * Moze existovat iba jeden, preto pri remove() sa musi znizit count na 0, 
 *    aby bolo mozne vytvorit dalsi
 */
public class GridBrokerGlobal extends NetworkNode {
    private static int count = 0;

    private static final String _jedinecnost = "Only one global broker is allowed!";
    private static final String _obmedzenie = 
            "Global broker must be connected to one global router!";
    
    /**
     * Ak uz GridBrokerGlobal.count == 1 tak vyhodi exception
     */
    public GridBrokerGlobal() throws Exception {
        super("GridBroker",0,0);
        if (count > 0)
            throw new Exception(_jedinecnost);
        count++;
    }

    
    /**
     * Vracia true, ak je mozne vytvorit novu instanciu triedy.
     */
    public static boolean canCreateNewInstance() {
        return count == 0;
    }
    
    /**
     * <font face="">vracia null, ak je mozne vytvorit novu linku z daneho uzla
     * (este 
     *    sa nevie, aky bude koniec linky). Inak vracia dovod, preco sa nemoze 
     *    vytvorit
     * </font>
     */
    public String canCreateNewLink() {
        if (getLinks().size() == 0)
            return null;
        else
            return new String(_obmedzenie);
    }

    /**
     * Vracia null, ak moze vytvorit spojenie s danym elementom. Inak vracia
     * dovod, 
     *    preco nie. Kontroluje aj, ci uz linka s danym elementom neexistuje.
     */
    public String canCreateNewLink(NetworkNode end) {
        if ((getLinks().size() == 0) && (end instanceof GridRouterGlobal))
            return null;
        else
            return new String(_obmedzenie);
    }

    /**
     * Odstranenie uzlu z topologie, spolu s jeho incidentnymi hranami
     */
    public void remove() {
        super.remove();
        count--;
    }
    
    /**
     * Vytvorenie globalneho brokera podla vlastnych parametrov
     */
    public BrokerHierarchical createBrokerGlobal() throws Exception {
        BrokerHierarchical broker = new BrokerHierarchical(
                getName(),
                getLinks().iterator().next().createSimpleLink(),
                null
                );
        
        return broker;
    }

    public LinkedList<Sim_entity> createGridSimEntities(String globalBrokerName, boolean trace_flag) throws Exception {
        LinkedList<Sim_entity> result = new LinkedList<Sim_entity>();
        //do vysledku sa nedava ziadna entita, pretoze sa vytvara inde        
        return result;
    }
}
